package bsts.view.complaint;
import javax.swing.*;
import bsts.application.BSTSApplication;
import bsts.controller.ComplaintController;
import bsts.model.*;
import bsts.view.util.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */

public class AddComplaintFrm extends JFrame implements ActionListener{
	
	/*Local attributes=================================================================================================*/
	//required as it references serializable objects for binary saving
	private static final long serialVersionUID = 1L;
	//System manager
	private BSTSApplication manager;
	//UI Related Variables
	public FieldPanel fpCustNric,fpComplaint;
	private JPanel btnPanel;
	
	/*Constructors=======================================================================================================*/
	public AddComplaintFrm(String pnlName, int width, int height, BSTSApplication manager) {
		this.manager = manager;
		setTitle(pnlName);
		setSize(width,height);	
		setResizable(false);		
		initialize();
	}
	
	//initialize the contro in this page=================================================================================*/
	private void initialize(){
		
		 //Field Panels ***********************************************************************
		 //create customer NRIC Field Panel
		 fpCustNric = new FieldPanel(FieldPanel.PnlType.TextField,"Customer Nric","",26,150,250);
		 //Field Panel class do not cater for additional button at the side so we have to add this button from here
		 fpCustNric.add(createBtnWithListener("Search",this,"search"));
		 //to get the control of field panel and set editable to false, since complaint can only be log by existing customer
		 JTextField customer_id_tf = (JTextField)fpCustNric.getControl();
		 customer_id_tf.setEditable(false);
		 
		//Create complaint field panel		 
		 fpComplaint = new FieldPanel(FieldPanel.PnlType.TextArea,"Complaint","",150,150,380);
		 
		JPanel fields = new JPanel(new FlowLayout(FlowLayout.LEFT));
		fields.setPreferredSize(new Dimension(590,280));
		fields.setBorder(BorderFactory.createTitledBorder("Log Complaint"));
		fields.add(fpCustNric);
		fields.add(fpComplaint);
		
		 //Button Panels ***********************************************************************
		 btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
		 btnPanel.setPreferredSize(new Dimension(600, 40));
		 btnPanel.add(createBtnWithListener("Add",this,"add"));
		 btnPanel.add(createBtnWithListener("Close",this,"close"));

		 //Create add frm panel
		 getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		 getContentPane().add(fields);
		 getContentPane().add(btnPanel);		
	}
	
	//wrapper class to basically wrap lines of code
    private static JButton createBtnWithListener(String text, ActionListener list, String cmd){
    	JButton btn = new JButton(text);
    	btn.addActionListener(list);
    	btn.setActionCommand(cmd);
    	return btn;
    }

    private boolean validateSave(){
    	String nric = fpCustNric.getValue().replace(" ", "");
    	String complaint_text = fpComplaint.getValue();
    	    
    	if (nric.equals("")){
    		JOptionPane.showMessageDialog(this, "Customer NRIC cannot be empty");
    		return false;
    	}else if (complaint_text.length()<11){
    		JOptionPane.showMessageDialog(this, "Complaint should not contain less than 10 characters(including space)");
    		return false;
    	}else{
    		return true;
    	}
    	
    }
    
    /*Public methods ===================================================================================================*/
    public void actionPerformed(ActionEvent evt){
    if(evt.getActionCommand().equals("add")){
		//boolean status;
    	if (validateSave()== true){
        	Complaint complaint;    
        	String complaint_text = fpComplaint.getValue();
        	String nric = fpCustNric.getValue();
    		int complaint_id = ComplaintController.getNext_ComplaintID();
    		complaint = new Complaint(Long.valueOf(complaint_id), new Date(),complaint_text,ComplaintStatus.Pending,manager.getLoginUser().getUserId(),nric);		
    		new ComplaintController().AddComplaint(complaint);    	
    		JOptionPane.showMessageDialog(this, "Complaint Added Successfully");
    		this.dispose();    		
    	}

    }else if (evt.getActionCommand().equals("search")){
    	SearchCustomerFrm custFrm = new SearchCustomerFrm(fpCustNric);
    	custFrm.setVisible(true);
    }else if (evt.getActionCommand().equals("close")){
    	this.dispose();
    }
	}


}
